package Leetcode第二期;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author : K k
 * @date : 11:34 2020/10/9
 */
public class 搜索旋转排序数组II_81 {
    public boolean search(int[] nums, int target) {

        if (nums==null || nums.length==0){
            return false;
        }
        //二分
        int left=0;
        int right=nums.length-1;
        while (left<=right){
            //查找旋转点
            int mid=left+((right-left)>>1);
            if (nums[mid]==target){
                return true;
            }
            if (nums[left]==nums[mid]){
                //存在重复
                left++;
                continue;
            }
            if (nums[left]<nums[mid]){
                //说明左边为升序
                if (nums[left]<=target && target<=nums[mid]){
                    //说明目标值在左边的序列中
                    right=mid-1;
                }else {
                    left=mid+1;
                }
            }else {
                //说明右边为升序序列
                if (nums[mid]<=target && target<=nums[right]){
                    left=mid+1;
                }else {
                    right=mid-1;
                }
            }
        }
        //一直没找到
        return false;
    }

    public static void main(String[] args) {
        搜索旋转排序数组II_81 x=new 搜索旋转排序数组II_81();
        x.search(new int[]{1,3},2);
    }
}
